package com.zy.ai.tools;

import com.zy.ai.service.AiVectorService;
import com.zy.service.IAiQuestionService;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Slf4j
@Component
@RequiredArgsConstructor
public class QuestionTools {

    private final IAiQuestionService aiQuestionService;
    private final AiVectorService aiVectorService;

    /**
     * 工具：记录问题与答案
     */
    @Tool("记录问题与答案，并生成向量")
    public void saveQuestion(@P("问题") String question, @P("答案") String answer) {
        // 生成向量
        List<Double> embedding = aiVectorService.getEmbedding(question);
        // 保存到数据库或向量库
        aiQuestionService.saveQuestionEmbedding(question, answer, embedding);
        log.info("问题已保存: {}", question);
    }

    /**
     * 工具：检索相似问题
     */
    @Tool("检索最相似的问题")
    public List<Map<String, Object>> searchSimilarQuestions(@P("问题") String question, @P("返回条数") int topK) {
        // 生成向量
        List<Double> embedding = aiVectorService.getEmbedding(question);
        // 查询向量库
        return aiVectorService.searchSimilar("ai_vectors", embedding, topK);
    }
}
